package mysgoog.pattern.StartGame;

import java.util.*;

import mysgoog.board.Board;
import mysgoog.bot.*;
import mysgoog.gtp.*;
import mysgoog.pattern.Matcher;
import mysgoog.pattern.Storage.VertexValue;
/**
* @author Fu, Zhongyang
*/
public class StartGamePatternMatcher implements Matcher
{

    private final int patternBoardSize = 8;
    private final int patternBoardSizeNum = 64;
    private final int patternNum = 4;

    private final int LEFT_UP = 0;
    private final int RIGHT_UP = 1;
    private final int LEFT_DOWN = 2;
    private final int RIGHT_DOWN = 3;

    private int[][] patternPos = null;

    Map<String, Integer> patternMap;
    public StartGamePatternMatcher()
    {
        patternMap = new HashMap<String, Integer>();
        StartGamePatternLoader.load(patternMap);
        genPatternBoard();
    }

    public List<VertexValue> getMatch(Board b, Color player, List<VertexValue> moves)
    {        
    	Color[] board = b.getBoard();
        int move;
        for (int i = 0; i < patternNum; i++)
        {
            int[] pos = getPatternBoard(board, i);
            move = getMove(pos, board, player);
            if (move != -1){
            	moves.add(new VertexValue(
            			new Vertex(Config.getEmptyBoard().posX(move), Config.getEmptyBoard().posY(move)),
            			100));
            }
        }
        return moves;
    }

    public void genPatternBoard()
    {
        patternPos = new int[patternNum][];
        for (int i = 0; i < patternNum; i++)
            patternPos[i] = new int[patternBoardSize * patternBoardSize];
        for (int i = 0; i < patternBoardSize; i++)
        {
            for (int j = 0; j < patternBoardSize; j++)
            {
                int pos = i * patternBoardSize + j;
                patternPos[LEFT_UP][pos] = Config.getEmptyBoard().makePos(patternBoardSize - j - 1, i);
                patternPos[RIGHT_UP][pos] = Config.getEmptyBoard().makePos(i, Config.getBoardSize() - patternBoardSize + j);
                patternPos[LEFT_DOWN][pos] = Config.getEmptyBoard().makePos(Config.getBoardSize() - i - 1, patternBoardSize - j - 1);
                patternPos[RIGHT_DOWN][pos] = Config.getEmptyBoard().makePos(Config.getBoardSize() - j - 1, Config.getBoardSize() - patternBoardSize + i);
                // if (patternPos[LEFT_UP][pos] >= 13*13)
                // System.out.println("LEFT_UP " + i + " " + j);
                // if (patternPos[RIGHT_UP][pos] >= 13*13)
                // System.out.println("RIGHT_UP " + i + " " + j);
                // if (patternPos[LEFT_DOWN][pos] >= 13*13)
                // System.out.println("LEFT_DOWN " + i + " " + j);
                // if (patternPos[RIGHT_DOWN][pos] >= 13*13)
                // System.out.println("RIGHT_DOWN " + i + " " + j);
            }
        }
    }

    public int[] getPatternBoard(Color[] board, int type)
    {
        return patternPos[type];
    }

    private int getMove(int[] pos, Color[] board, Color player)
    {
        String pattern = "";
        for (int i = 0; i < patternBoardSizeNum; i++)
        {
            // System.out.println(pos[i] + " ");
            if (!(board[pos[i]]).isFilled())
                pattern += ".";
            else
            {
                if (board[pos[i]] == player)
                    pattern += "O";
                else
                    pattern += "X";
            }
        }
        Integer move = patternMap.get(pattern);
        if (move == null)
            return -1;
        else
            return pos[move];
    }

}
